package com.xiaozhen.driver.controller;

import com.xiaozhen.common.core.domain.driver.ParseDriver;
import com.xiaozhen.security.controller.BaseController;
import com.xiaozhen.common.core.domain.AjaxResult;
import com.xiaozhen.common.core.domain.driver.ParseTcpDriver;
import com.xiaozhen.driver.service.ParseTcpDriverService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/9/29 10:08
 * @description:
 */
@RestController
@Api(tags = "TCP驱动管理接口")
@RequestMapping("/tcpDiver")
public class ParseTcpDriverController extends BaseController {

    @Autowired
    private ParseTcpDriverService tcpDriverService;

    @ApiOperation("添加驱动")
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody ParseTcpDriver tcpDriver) {
        tcpDriver.setCreateBy(getCurrentUserId());
        tcpDriver.setCreateTime(LocalDateTime.now());
        return toAjax(tcpDriverService.insertDriver(tcpDriver));
    }

    @ApiOperation("修改驱动")
    @PutMapping("/update")
    public AjaxResult edit(@Validated @RequestBody ParseTcpDriver tcpDriver) {
        tcpDriver.setUpdateBy(getCurrentUserId());
        tcpDriver.setUpdateTime(LocalDateTime.now());
        return toAjax(tcpDriverService.updateDriver(tcpDriver));
    }

    @ApiOperation("删除驱动")
    @DeleteMapping("/{driverId}")
    public AjaxResult remove(@PathVariable("driverId") Long driverId) {
        return toAjax(tcpDriverService.deleteDriverById(driverId));
    }

    @ApiOperation("根据ID获取驱动信息")
    @GetMapping("/getDriverById")
    public AjaxResult getDriverById(Long driverId) {
        ParseTcpDriver tcpDriver = tcpDriverService.getDriverById(driverId);
        return AjaxResult.success(tcpDriver);
    }
}
